<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class Finance extends Controller
{
    public function index()
    {
        $mchA = [5=>'1571615431',12=>'1567597801'];//可赞
        $mchB = [17=>'1609226509',24=>'1609549160'];//B座
        $startDate = '2022-01-01';
        $endDate = '2022-01-19';
        foreach($mchA as $k=>$v)
        {
            $mchA[$v]['sum'] = $this->getFinanceDataByMchid($k,$startDate,$endDate);
        }
        foreach($mchB as $k=>$v)
        {
            $mchB[$v]['sum'] = $this->getFinanceDataByMchid($k,$startDate,$endDate);
        }
        $this->assign([
            'mchA'=>$mchA,
            'mchB'=>$mchB,
        ]);
        return view();
    }
    
    
    private function getFinanceDataByMchid($id,$startDate,$endDate)
    {
        $pay_uniacid = $id;
        if($pay_uniacid == 77)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',77)->where('uniacid','<',177)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$id:$id;
        }
        if($pay_uniacid == 37)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',40)->where('uniacid','<',77)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$id:$id;
        }
        return Db::connect("live_db_config")->table('ims_wzbagent_giftlogs')->whereTime('dateline', 'between', [$startDate, $endDate]) ->where('status',1)->where('pay_uniacid','IN',$pay_uniacid)->value('sum(amount)')/100;
    }
    
    public function profitSharingMchList()
    {
        /*$mch[0] = [
            
            5=>['mch_id'=>'1571615431','data'=>$this->profitSharingGetMchData(5,0),'pay_uniacid'=>5],
            12=>['mch_id'=>'1567597801','data'=>$this->profitSharingGetMchData(12,0),'pay_uniacid'=>12],
            ];
        $mch[1] = [
                17=>['mch_id'=>'1609226509','data'=>$this->profitSharingGetMchData(17,0),'pay_uniacid'=>17],
                24=>['mch_id'=>'1609549160','data'=>$this->profitSharingGetMchData(24,0),'pay_uniacid'=>24],
                30=>['mch_id'=>'1626951488','data'=>$this->profitSharingGetMchData(30,0),'pay_uniacid'=>30],
                //31=>['mch_id'=>'1627545180','data'=>$this->profitSharingGetMchData(31,0),'pay_uniacid'=>31]
                ];*/
                
        $mch[0] = $mch[1] = 0;
        $mch[2] = [
            25=>['mch_id'=>'1620905287','data'=>$this->profitSharingGetMchData(25,0),'pay_uniacid'=>25],
            33=>['mch_id'=>'1623269128','data'=>$this->profitSharingGetMchData(33,0),'pay_uniacid'=>33],
            77=>['mch_id'=>'1634480723','data'=>$this->profitSharingGetMchData(77,0),'pay_uniacid'=>77],
            147=>['mch_id'=>'1635811722','data'=>$this->profitSharingGetMchData(147,0),'pay_uniacid'=>147],
        ];
        $mch[3] = [
            35=>['mch_id'=>'1630759818','data'=>$this->profitSharingGetMchData(35,0),'pay_uniacid'=>35],
            36=>['mch_id'=>'1632092555','data'=>$this->profitSharingGetMchData(36,0),'pay_uniacid'=>36],
            37=>['mch_id'=>'1634435244','data'=>$this->profitSharingGetMchData(37,0),'pay_uniacid'=>37],
            314=>['mch_id'=>'1638862926','data'=>$this->profitSharingGetMchData(314,0),'pay_uniacid'=>314],
            315=>['mch_id'=>'1639874070','data'=>$this->profitSharingGetMchData(315,0),'pay_uniacid'=>315]
        ];
        $this->assign([
            'mch'=>$mch,
        ]);
        return view();
    }
    
    public function profitSharingOrderList()
    {
        if(input()['limit'])
        {
            $page = input()['page'];
            $limit = input()['limit'];
            $pay_uniacid = input()['pay_uniacid'];
            $keyword = input()['keyword'];
            $list = Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->alias('s')->field('s.*,l.title,w.name as wxapp_name')->join('ims_wzbagent_wxapp w','w.id=s.wxapp_id')->join('ims_wzbagent_lives l','l.id=s.live_id')->where('s.pay_uniacid',$pay_uniacid)->where('l.title|w.name|s.transaction_id|s.order_id','LIKE',"%$keyword%")->where('s.is_small',0);
            $count = Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->alias('s')->field('s.*,l.title,w.name as wxapp_name')->join('ims_wzbagent_wxapp w','w.id=s.wxapp_id')->join('ims_wzbagent_lives l','l.id=s.live_id')->where('s.pay_uniacid',$pay_uniacid)->where('l.title|w.name|s.transaction_id|s.order_id','LIKE',"%$keyword%")->where('s.is_small',0);
            if(input()['status'] !='' )
            {
                $list = $list->where('s.status',input()['status']);
                $count = $count->where('s.status',input()['status']);
            }
            $list = $list->page($page,$limit)->order('s.id DESC')->select();
            $count = $count->count();
            
            foreach ($list as $k=>$v)
            {
                $list[$k]['order_amount'] = $v['order_amount']/100;
                $list[$k]['sharing_amount'] = $v['sharing_amount']/100;
                $list[$k]['status_text'] = $v['status']?'已分账':'待分账';
                $list[$k]['creat_time'] = date('Y-m-d H:i',$v['creat_time']);
                $list[$k]['finish_time'] = $v['finish_time']?date('Y-m-d H:i',$v['finish_time']):'';
            }
            return ['code'=>0,'count'=>$count,'data'=>$list,'msg'=>''];
        }
        else
        {
            return view();
        }
    }
    
    public function doProfitSharing()
    {
        if( in_array(date('H'),['10,11,20,21']) && input('auto') )
        {
            return json_encode(['code'=>1,'msg'=>'投票高峰期禁止使用']);
        }
        set_time_limit(0);
        $mchData = [
                12=>['openid'=>'otTED47-C_2f_nDjg6jA7IaVVmEE','appid'=>'wx6cdea79ce799ada4','mch_id'=>'1567597801','mch_secrect'=>'16893E36DD2DF093513B508FBBCF3BF6'],
                5=>['openid'=>'oJNDS5cALO-NAndV3uW1khGu6dNg','appid'=>'wx1308d1e7d62384e8','mch_id'=>'1571615431','mch_secrect'=>'16893E36DD2DF093513B508FBBCF3BF6'],
                
                25=>['openid'=>'oNPiu5JTBuuo13BJmDfDuLuR858A','appid'=>'wx9a562258b4fe5546','mch_id'=>'1620905287','mch_secrect'=>'muj4rqngzplib0t21mr41i11iltkhzbv'],
                33=>['openid'=>'ortqL4qAxrtjsDfjrhComfQMlbaQ','appid'=>'wxf793f375e97faa04','mch_id'=>'1623269128','mch_secrect'=>'d4uepvjoant2uxcxbgwekslbn2vf3viu'],
                
                24=>['openid'=>'ogpbF4pwxjhFlX7WGGSqGwoQY5Mo','appid'=>'wx1116df9b94b6bd93','mch_id'=>'1609549160','mch_secrect'=>'uc0kxqezvhwwmo2fqrvlqnz15dwh04yf'],
                17=>['openid'=>'ogpbF4pwxjhFlX7WGGSqGwoQY5Mo','appid'=>'wx1116df9b94b6bd93','mch_id'=>'1609226509','mch_secrect'=>'16893E36DD2DF093513B508FBBCF3BF6'],
                
                30=>['openid'=>'oKM4m5ALgWeHmA1f4dl4xE2pXacI','appid'=>'wx6529a491d9824e20','mch_id'=>'1626951488','mch_secrect'=>'ytahyvputnuzd0acnslilyl0dknowtza'],

                31=>['openid'=>'orEKV5RkAR1SgDj7KoCc4-9iP7cU','appid'=>'wx8f55dc7acef8842d','mch_id'=>'1627545180','mch_secrect'=>'sf4pisvhb3ylcvmbxq3hapopultbrf1d'],
                
                35=>['openid'=>'oUUEc5e-bac4mUhMNvKt0W9j9x0g','appid'=>'wx853fb3dade28476b','mch_id'=>'1630759818','mch_secrect'=>'pkau411w3hkaylbpb55uvsla23pceegt'],
                36=>['openid'=>'on5o35HBbFPqueiksiYB2J-HYuCU','appid'=>'wxfedfeb78b441bf26','mch_id'=>'1632092555','mch_secrect'=>'pkau411w3hkaylbpb55uvsla23pceegt'],
                37=>['openid'=>'oN__T5ZdYQ9u3DDvxfAqIa3Km3pY','appid'=>'wxfa94657fe6521847','mch_id'=>'1634435244','mch_secrect'=>'pkau411w3hkaylbpb55uvsla23pceegt'],
                77=>['openid'=>'oRXHv5Gdc0_gW47kE9G1ZPKD8isw','appid'=>'wxf1d9a5e4b7441488','mch_id'=>'1634480723','mch_secrect'=>'pkau411w3hkaylbpb55uvsla23pceegt'],
                34=>['openid'=>'oPHtR6cS2brS3xzzH4Uy1_LUCEiY','appid'=>'wx896cb6302187d842','mch_id'=>'1635738483','mch_secrect'=>'yo5hsqrdfifnvj3py514onyxkpxcmibu'],
                34=>['openid'=>'oPHtR6cS2brS3xzzH4Uy1_LUCEiY','appid'=>'wx896cb6302187d842','mch_id'=>'1635738483','mch_secrect'=>'yo5hsqrdfifnvj3py514onyxkpxcmibu'],
                254=>['openid'=>'o5SPI6HcgF-SXittSxMuErpVy7w8','appid'=>'wx1a363cdd0a2a5658','mch_id'=>'1636300633','mch_secrect'=>'yo5hsqrdfifnvj3py514onyxkpxcmibu'],
                
                7=>['openid'=>'oLYZF5MQNnwWEPDLm4CISxzC42Vg','appid'=>'wx5b1b61865206bfdc','mch_id'=>'1581054411','mch_secrect'=>'16893E36DD2DF093513B508FBBCF3BF6'],
                13=>['openid'=>'oQm-k5ekiAX73CK3wrzgBHCzj4bI','appid'=>'wx73d257f00aa4e670','mch_id'=>'1549401141','mch_secrect'=>'buFLzNXxXtxNS3WxFIk3HNVboqLFeOCA'],
                32=>['openid'=>'oq-Aq5bqcNTbKJwgqnNokKCgURZk','appid'=>'wx3f91df156e9cf812','mch_id'=>'1627368099','mch_secrect'=>'tfbganhnc0ltd01vgzmozkqexv5bh5ew'],
                39=>['openid'=>'oG5gN5UaGmU_v3aHWSL7oy3J_MvI','appid'=>'wx29f3327f4ca22ea2','mch_id'=>'1634533446','mch_secrect'=>'1qcynmptaxfaetd41ofealubrzsanx0t'],
                314=>['openid'=>'ousAN5SjNs0cozePqn6cn2uEoV20','appid'=>'wxbc26b7ac836b9b6b','mch_id'=>'1638862926','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
                315=>['openid'=>'oLgez5PhEFx4VwhojbkuW3lCTg4A','appid'=>'wxfadfe1fa3773bdb2','mch_id'=>'1639874070','mch_secrect'=>'oJfza521TGMsRdf3nZFXZUjzc8x8HTJY'],
            ];
        $pay_uniacid = $payUniacid = input()['pay_uniacid'];
        if($pay_uniacid == 315)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','=',315)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 314)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','=',314)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 77)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',77)->where('uniacid','<',147)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 147)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',146)->where('uniacid','<',247)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 37)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',40)->where('uniacid','<',77)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 39)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',204)->where('uniacid','>',40)->where('uniacid','<',115)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 34)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',204)->where('uniacid','>',152)->where('uniacid','<',352)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        if($pay_uniacid == 254)
        {
            $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',204)->where('uniacid','>',253)->where('uniacid','<',454)->value('GROUP_CONCAT(uniacid)');
            $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
        }
        $list = Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->where('is_small',0)->where('pay_uniacid','IN',$pay_uniacid)->where('status',0)->order('id desc')->limit(0,200)->select();
        //var_dump($list);exit;
        $time = time();
        $successCount = 0;
        $totalCount = count($list);
        //var_dump($list);exit;
        foreach ($list as $k=>$v)
        {
            $mch_id = $mchData[$payUniacid]['mch_id'];
            $mch_secrect = $mchData[$payUniacid]['mch_secrect'];
            $appid = $mchData[$payUniacid]['appid'];
            $openid = $mchData[$payUniacid]['openid'];
            $transaction_id = $v['transaction_id'];
            $out_order_no = $v['out_order_no'];
            $amount = $v['sharing_amount'];
            /*if($amount == 0){
                //echo json_encode(['code'=>0,'successCount'=>0,'totalCount'=>0,'msg'=>'分账金额不合法']);
                continue;
            }*/
            if($pay_uniacid == 310)
                $result = controller('admin/Weixin9782')->fwsProfitsharing($mch_id,$appid,$transaction_id,$out_order_no,$openid,$amount,$mch_secrect);
            else
                $result = controller('admin/Weixin9782')->profitsharing($mch_id,$appid,$transaction_id,$out_order_no,$openid,$amount,$mch_secrect);
            //var_dump($amount);exit;
            var_dump($result);
            if($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS')
            {
                $sqlStatus = Db::connect("live_db_config")->table('ims_wzbagent_profit_sharing')->where('is_small',0)->where('id',$v['id'])->update(['status'=>1,'order_id'=>$result['order_id'],'finish_time'=>$time]);
                if($sqlStatus)
                    $successCount ++;
            }
            //var_dump($result,$mch_id,$appid,$transaction_id,$out_order_no,$openid,$amount,$mch_secrect);exit;
        }
        //print_r(['code'=>$code,'successCount'=>$successCount,'totalCount'=>$totalCount]);
        echo json_encode(['code'=>1,'successCount'=>$successCount,'totalCount'=>$totalCount]);
        
    }

    public function profitSharingGetMchData($pay_uniacid,$type = 1)
    {
        if($pay_uniacid)
        {
            $payUniacid = $pay_uniacid;
            if($pay_uniacid == 77)
            {
                $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',77)->where('uniacid','<',147)->value('GROUP_CONCAT(uniacid)');
                $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
            }
            if($pay_uniacid == 147)
            {
                $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',147)->where('uniacid','<',247)->value('GROUP_CONCAT(uniacid)');
                $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
            }
            if($pay_uniacid == 37)
            {
                $pay_uniacid = Db::connect("live_db_config")->table('ims_account_wechats')->where('type',203)->where('uniacid','>',40)->where('uniacid','<',77)->value('GROUP_CONCAT(uniacid)');
                $pay_uniacid = $pay_uniacid?$pay_uniacid.','.$payUniacid:$payUniacid;
            }
            $unProfitSharing = Db::connect("live_db_config")->field('sum(order_amount) as a1, sum(sharing_amount) as a2, count(*) as c')->table('ims_wzbagent_profit_sharing')->where('is_small',0)->where('status',0)->where('pay_uniacid','IN',$pay_uniacid)->find();
            if($type == 0)
                return $unProfitSharing;
                
            $profitSharing = Db::connect("live_db_config")->field('sum(order_amount) as a1, sum(sharing_amount) as a2, count(*) as c')->table('ims_wzbagent_profit_sharing')->where('is_small',0)->where('status',1)->where('pay_uniacid','IN',$pay_uniacid)->find();
            return (['code'=>1,'unProfitSharing'=>$unProfitSharing,'profitSharing'=>$profitSharing]);
        }
    }
    
    
}
    
    
    
    